Valid = {
	init: function (){
		$.validator.addMethod("func_not_empty", function(value, element) {
			console.log("call func_not_empty on " + value);
			value = value.replace(/\s+/g, "").replace(/\./g, "").replace(/\-/g, "").replace(/\:/g, "").replace(/\//g, "");
			return value != "";
		}, "This field can not be empty.");
		$.validator.addMethod("func_is_phone", function(value, element) { 
			console.log("call func_is_phone on " + value);
			if (this.optional(element)) {
				return true;
			}
			value = value.replace(/\s+/g, "");
	        return value.length > 6 &&
	        	value.match(/^\d{2,4}\-\d{6,20}$/);
		}, "Please enter a valid phone number.");
		$.validator.addMethod("func_is_price", function(value, element) { 
			console.log("call func_is_price on " + value);
			if (this.optional(element)) {
				return true;
			}
			value = value.replace(/\s+/g, "");
	        return value.match(/^\d{1,10}\.\d{1,2}$/);
		}, "Please enter a valid price.");
		$.validator.addMethod("func_is_date", function(value, element) { 
			console.log("call func_is_date on " + value);
			if (this.optional(element)) {
				return true;
			}
	        return value.match(/^(0?[1-9]|1[0-2])\/(0?[1-9]|[1-2]\d|3[0-1])\/\d{4}$/);
		}, "Please enter a valid date.");
		$.validator.addMethod("func_is_time", function(value, element) { 
			console.log("call func_is_time on " + value);
			if (this.optional(element)) {
				return true;
			}
	        return value.match(/^([0-2][0-9]):([0-5][0-9]):([0-5][0-9])\s(AM|PM)$/);
		}, "Please enter a valid time.");
		
		$.validator.addClassRules({
			func_is_phone: {
				func_is_phone: true
			},
			func_not_empty: {
				func_not_empty: true
			},
			func_is_price: {
				func_is_price: true
			},
			func_is_date: {
				func_is_date: true
			},
			func_is_time: {
				func_is_time: true
			}
		});
	},
	data: function (filedSets){
		filedSets.each(function(index, domEle){
			var fieldId = $(domEle).attr("id").replace("-fieldset", "");
			var valid = Vari.fieldMap.get(fieldId).get("valid");
			var formKey = $(domEle).find(".formKey");
			
			if (formKey.length > 0) {			
				if ($(domEle).hasClass("multipleChoice")) {
					var checked = $(domEle).find(":radio:checked");				
					formKey.val(checked.val());					
				} else if ($(domEle).hasClass("checkboxes")){
					var checked = $(domEle).find(":checkbox:checked");
					var v = "";
					checked.each(function() {
						var t = $(this).val();
						if (v == "" && Util.isNotEmpty(t)) {
							v += t;
						} else if (v != "" && Util.isNotEmpty(t)) {
							v += "," + t;
						}
					});
					formKey.val(v);
					
				} else if ($(domEle).hasClass("name") || $(domEle).hasClass("address")){
					var elements = $(domEle).find(".f");
					var v = "";					
					elements.each(function() {
						var t = $(this).val();
						if (v == "" && Util.isNotEmpty(t)) {
							v += t;
						} else if (v != "" && Util.isNotEmpty(t)) {
							v += "," + t;
						}
					});
					formKey.val(v);
					
				} else if ($(domEle).hasClass("date")){
					var elements = $(domEle).find(".f");
					var v = $(elements[0]).val() + "/" + $(elements[1]).val() + "/" + $(elements[2]).val();
					formKey.val(v);
					
				} else if ($(domEle).hasClass("time")){
					var elements = $(domEle).find(".f");
					var v = $(elements[0]).val() + ":" + $(elements[1]).val() 
						+ ":" + $(elements[2]).val()
						+ " " + $(elements[3]).val();
					formKey.val(v);
					
				} else if ($(domEle).hasClass("price")){
					var elements = $(domEle).find(".f");
					var v = $(elements[0]).val() + "." + $(elements[1]).val();
					formKey.val(v);					
				} else if ($(domEle).hasClass("phone")){
					var elements = $(domEle).find(".f");
					var v = $(elements[0]).val() + "-" + $(elements[1]).val();
					formKey.val(v);					
				} else {
					var elements = $(domEle).find(".f");
					var v = "";
					elements.each(function() {
						var t = $(this).val();
						if (v == "" && Util.isNotEmpty(t)) {
							v += t;
						} else if (v != "" && Util.isNotEmpty(t)) {
							v += "," + t;
						}
					});
					formKey.val($.trim(v));
				}
			}
		});		
	}
};